<?php
/**
 * Created by PhpStorm.
 * User: lzy 林子彦
 * Date: 2018/6/1
 * Time: 上午1:09
 */
namespace App\Admin\Controllers;
use App\Admin\Contracts\ConfigInterface;
use App\Base\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;

/**
 *  后台系统配置
 * Class MenuController
 * @package App\Admin\Controllers
 */
class ConfigController extends Controller
{
    /**
     * @return ConfigInterface
     */
    public function getConfigInterface(){
        return app()->make(ConfigInterface::class);
    }

   public function group(Request $request){
       if ($request->getMethod() == "POST") {


           $where=function ($query) use ($request){
               $title=$request->input("title","");
               $name=$request->input("name","");
               $type=$request->input("type","");


               if(!empty($title)){
                   $query->where("title","like",'%'.$title.'%');
               }
               if(!empty($name)){
                   $query->where("name","like",'%'.$name.'%');
               }
               if(!empty($type)){
                   $query->where("type",$type);
               }
           };

           $list=$this->getConfigInterface()->getConfigList($where);
           $count=$this->getConfigInterface()->getConfigCount($where);
           echo  json_encode( ['code'=>0,'status'=>200,'count'=>$count,'data'=>$list]);  //改天在封装
           exit;
        }
       $menuList=$this->getConfigInterface()->getConfigClassMenuList([]);
       $menuArr=array();
       foreach ( $menuList as $k=> $item) {
           $menuArr[$k]["id"]=$item->id;
           $menuArr[$k]["name"]=$item->name;
           $menuArr[$k]["pId"]=$item->pid;
       }
       return view("admin.config.group")->with("menu_list",json_encode($menuArr));
   }

    /**
     * 配置添加
     * @param Request $request
     * @return $this|mixed
     */
   public function add(Request $request){
       if ($request->getMethod() == "POST") {
           return $this->getConfigInterface()->saveConfigData($request->all());
       }
       $menuid = $request->input('menuid');
       if (empty(intval($menuid))) {
          return   $this->show_error('请先在页面左侧选择参数分类');
       } else {
           //加载模板
           return view("admin.config.add")->with("menuid",$menuid);
       }
   }

    /**
     * 配置编辑
     * @param Request $request
     * @return $this|mixed
     */
    public function edit(Request $request){
        if ($request->getMethod() == "POST") {
            return $this->getConfigInterface()->saveConfigData($request->all());
        }
        $id=$request->route('id');
        if (empty(intval($id))) {
            return   $this->show_error('请选择要修改字段');
        } else {
            $info=$this->getConfigInterface()->getConfigInfo(["id"=>$id]);
            //加载模板
            return view("admin.config.edit")->with("info",$info);
        }
    }

    public function del(Request $request){
        $validator=Validator::make($request->all(), [
            'id' => 'required',
        ],["id.required"=>'删除参数id是必须的']);
        if ($validator->fails()) {
             $this->throwValidatorException($validator);
        }
        if ($request->getMethod() == "POST") {
            return $this->getConfigInterface()->delConfigData($request->all());
        }
    }

    public function classIndex(Request $request){
        $menuList=$this->getConfigInterface()->getConfigClassMenuList([]);
        $menuArr=array();
        foreach ( $menuList as $k=> $item) {
            $menuArr[$k]["id"]=$item->id;
            $menuArr[$k]["name"]=$item->name;
            $menuArr[$k]["pId"]=$item->pid;
        }
        return view("admin.config.class_index")->with("menu_list",json_encode($menuArr));
    }

    public function classEdit(Request $request){
        return $this->getConfigInterface()->getConfigClassInfo($request->all());
    }

    /**
     * 保存分类
     * @param Request $request
     * @return mixed
     */
    public function classSave(Request $request){
        return $this->getConfigInterface()->updateOrCreateConfigClassData($request->all());

    }

    /**
     * 删除配置分类
     * @param Request $request
     * @return mixed
     */
    public function classDel(Request $request){
        return $this->getConfigInterface()->delConfigClass($request->all());
    }

    /**
     * 选择菜单
     * @param Request $request
     */
    public function classSelectMenus(Request $request){
        $menuList=$this->getConfigInterface()->getConfigClassMenuList([]);
        $menuArr=array();
        foreach ( $menuList as $k=> $item) {
            $menuArr[$k]["id"]=$item->id;
            $menuArr[$k]["name"]=$item->name;
            $menuArr[$k]["pId"]=$item->pid;
        }
        return view("admin.config.show_select_menus")->with("menu_list",json_encode($menuArr));
    }

}